<?php


namespace app\reception\server;


class MiHuaPayServer
{
    // 米花支付-验证签名解密数据
    public function mhCheckSign($encodeData)
    {
        include_once('../application/reception/extend/MiHuaApi/config.php');
        include_once('../application/reception/extend/MiHuaApi/getSign.php');
        include_once('../application/reception/extend/MiHuaApi/getData.php');

        $publicKey = \MiHuaPayConfig::PUBLIC_KEY;
        $res = decryptData($encodeData, $publicKey);
        $decryptData = json_decode($res, true);
        if (checkMhSign($decryptData, $publicKey)) {
            return $decryptData;
        } else {
            return array();
        }
    }

    // 米花支付-验证签名解密数据
    public function newMhCheckSign($encodeData, $config)
    {
        include_once('../application/reception/extend/MiHuaApi/config.php');
        include_once('../application/reception/extend/MiHuaApi/getSign.php');
        include_once('../application/reception/extend/MiHuaApi/getData.php');

        $publicKey = $config['public_key'];
        $res = decryptData($encodeData, $publicKey);
        $decryptData = json_decode($res, true);
        if (checkMhSign($decryptData, $publicKey)) {
            return $decryptData;
        } else {
            return array();
        }
    }

    // 米花支付查询订单更详细的信息
    public function mhGetOrder($decryptData)
    {
        include_once('../application/reception/extend/MiHuaApi/config.php');
        include_once('../application/reception/extend/MiHuaApi/getSign.php');
        include_once('../application/reception/extend/MiHuaApi/getData.php');

        // 订单查询
        $orderData = array(
            'merAccount' => \MiHuaPayConfig::MER_ACCOUNT,
            'orderId' => $decryptData['orderId'],
            'time' => time(),
        );
        // 生成签名
        $privateKey = \MiHuaPayConfig::PRIVATE_KEY;
        $orderData['sign'] = getMhSign($orderData, $privateKey);
        $encodeData = encryptData($orderData, $privateKey);
        $postData = array(
            'merAccount' => \MiHuaPayConfig::MER_ACCOUNT, // 商户标识
            'data' => $encodeData,
        );
        $api_url = \MiHuaPayConfig::API_URL_GET_ORDER;
        $res = httpPost($api_url, http_build_query($postData));
        return json_decode($res, true);
    }

    // 米花支付查询订单更详细的信息
    public function newMhGetOrder($decryptData, $config)
    {
        include_once('../application/reception/extend/MiHuaApi/config.php');
        include_once('../application/reception/extend/MiHuaApi/getSign.php');
        include_once('../application/reception/extend/MiHuaApi/getData.php');

        // 订单查询
        $orderData = array(
            'merAccount' => $config['mer_account'],
            'orderId' => $decryptData['orderId'],
            'time' => time(),
        );
        // 生成签名
        $privateKey = $config['private_key'];
        $orderData['sign'] = getMhSign($orderData, $privateKey);
        $encodeData = encryptData($orderData, $privateKey);
        $postData = array(
            'merAccount' => $config['mer_account'], // 商户标识
            'data' => $encodeData,
        );
        $api_url = \MiHuaPayConfig::API_URL_GET_ORDER;
        $res = httpPost($api_url, http_build_query($postData));
        return json_decode($res, true);
    }
}